package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.service.MarketConfigMallService;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
@Service
public class MarketConfigMallServiceImpl implements MarketConfigMallService {
    @Transactional
    @Override
    public HashMap<String, String> getConfigMall() {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_mall_address",getValues("market_mall_address"));
        map.put("market_mall_latitude",getValues("market_mall_latitude"));
        map.put("market_mall_longitude",getValues("market_mall_longitude"));
        map.put("market_mall_name",getValues("market_mall_name"));
        map.put("market_mall_phone",getValues("market_mall_phone"));
        map.put("market_mall_qq",getValues("market_mall_qq"));
        return map;
    }
    @Transactional
    @Override
    public HashMap<String, String> getConfigExpress() {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_express_freight_min",getValues("market_express_freight_min"));
        map.put("market_express_freight_value",getValues("market_express_freight_value"));
        return map;
    }
    @Transactional
    @Override
    public HashMap<String, String> getConfigOrder() {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_order_comment",getValues("market_order_comment"));
        map.put("market_order_unconfirm",getValues("market_order_unconfirm"));
        map.put("market_order_unpaid",getValues("market_order_unpaid"));
        return map;
    }
    @Transactional
    @Override
    public HashMap<String, String> getConfigWx() {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_wx_catlog_goods",getValues("market_wx_catlog_goods"));
        map.put("market_wx_catlog_list",getValues("market_wx_catlog_list"));
        map.put("market_wx_index_brand",getValues("market_wx_index_brand"));
        map.put("market_wx_index_hot",getValues("market_wx_index_hot"));
        map.put("market_wx_index_new",getValues("market_wx_index_new"));
        map.put("market_wx_index_topic",getValues("market_wx_index_topic"));
        map.put("market_wx_share",getValues("market_wx_share"));
        return map;
    }
    @Autowired
    MarketSystemMapper mapper;
    private String getValues(String keyName) {
        MarketSystemExample example = new MarketSystemExample();
        example.or().andKeyNameEqualTo(keyName);
        List<MarketSystem> marketSystems = mapper.selectByExample(example);
        String keyValue = marketSystems.get(0).getKeyValue();
        return keyValue;
    }
    @Transactional
    @Override
    public void updataMall(String mallAddress, String marketMallLatitude, String marketMallLongitude, String marketMallName, String marketMallPhone, String marketMallQq) {
        upData("market_mall_address",mallAddress);
        upData("market_mall_latitude",marketMallLatitude);
        upData("market_mall_longitude",marketMallLongitude);
        upData("market_mall_name",marketMallName);
        upData("market_mall_phone",marketMallPhone);
        upData("market_mall_qq",marketMallQq);
    }
    @Transactional
    @Override
    public void updateExp(String marketExpressFreightValue, String marketExpressFreightMin) {
        upData("market_express_freight_min",marketExpressFreightMin);
        upData("market_express_freight_value",marketExpressFreightValue);
    }
    private void upData(String key, String values){
        MarketSystemExample example = new MarketSystemExample();
        example.createCriteria().andKeyNameEqualTo(key);
        MarketSystem marketSystem = new MarketSystem();
        marketSystem.setKeyValue(values);
        mapper.updateByExampleSelective(marketSystem,example);
    }
    @Transactional
    @Override
    public void upOrder(String marketOrderUnpaid, String marketOrderUnconfirm, String marketOrderComment) {
        upData("market_order_comment",marketOrderComment);
        upData("market_order_unconfirm",marketOrderUnconfirm);
        upData("market_order_unpaid",marketOrderUnpaid);
    }
    @Transactional
    @Override
    public void updateWx(String marketWxShare, String marketWxCatlogGoods, String marketWxCatlogList, String marketWxIndexBrand, String marketWxIndexHot, String marketWxIndexNew, String marketWxIndexTopic) {
        upData("market_wx_catlog_goods",marketWxCatlogGoods);
        upData("market_wx_catlog_list",marketWxCatlogList);
        upData("market_wx_index_brand",marketWxIndexBrand);
        upData("market_wx_index_hot",marketWxIndexHot);
        upData("market_wx_index_new",marketWxIndexNew);
        upData("market_wx_index_topic",marketWxIndexTopic);
        upData("market_wx_share",marketWxShare);
    }
}
